//
//  CQFoldCardCollectionViewFlowLayout.swift
//  TSListDemo
//
//  Created by qian on 2020/11/6.
//  Copyright © 2020 dvlproad. All rights reserved.
//
//  折叠效果的flowLayout

import UIKit

class CQFoldCardCollectionViewFlowLayout: CQTBaseCardCollectionViewFlowLayout {
    /// 每个 cell 在 y 之间的距离
    fileprivate let cellCenterOffset_y : CGFloat = 20.0
    
    override func getCellSize() -> CGSize {
        let collectionViewWidth = self.collectionView!.bounds.width
        let collectionViewHeight = self.collectionView!.bounds.height
        
        var cellHeight: CGFloat = collectionViewHeight - 80
        var cellWidth: CGFloat = cellHeight * 0.685
        if cellWidth > collectionViewWidth {
            debugPrint("宽度\(cellWidth)>\(collectionViewWidth)太多，自动调节")
            cellWidth = collectionViewWidth - 20
            cellHeight = cellWidth / 0.685
        }
        
        return CGSize.init(width: cellWidth, height: cellHeight)
    }
    
    override func getLastCellCenterY(itemIndex: NSInteger, scale: CGFloat) -> CGFloat {
        let collectionViewHeight: CGFloat = self.collectionView!.bounds.height
        
        let itemYSpacing: CGFloat = 20
        
        var cellOriginY : CGFloat = 20
        if itemIndex > 2 {
            cellOriginY += 2 * itemYSpacing
        } else {
            cellOriginY += CGFloat(itemIndex) * itemYSpacing
        }
        
        //let screemSizeRatio = UIScreen.main.bounds.size.width / 320
        //let cellHeightWithoutScale: CGFloat = collectionViewHeight-80*screemSizeRatio
        let cellHeightWithoutScale: CGFloat = collectionViewHeight-80
        let cellCenterYWithoutScale = cellOriginY + cellHeightWithoutScale/2
        
        let cellCenterY = cellCenterYWithoutScale + ( cellHeightWithoutScale / 2 * (1 - scale))
        
        //        var cellCenterYWithoutScale2 = CQFoldCardCollectionViewFlowLayout.__cellCenterYWithoutScale(itemIndex: itemIndex, itemYSpacing: itemYSpacing, cellHeightWithoutScale: cellHeightWithoutScale)
        //        var cellCenterYWithScale22 = CQFoldCardCollectionViewFlowLayout.__cellCenterYWithScale(cellCenterYWithoutScale: cellCenterYWithoutScale2, cellHeightWithoutScale: cellHeightWithoutScale, scale: scale)
        
        return cellCenterY
    }
    
//    class func __cellCenterYWithoutScale(itemIndex: NSInteger, itemYSpacing: CGFloat, cellHeightWithoutScale: CGFloat) -> CGFloat {
//        var cellOriginY : CGFloat = 20
//        if itemIndex > 2 {
//            cellOriginY += 2 * itemYSpacing
//        } else {
//            cellOriginY += CGFloat(itemIndex) * itemYSpacing
//        }
//
//        let cellCenterYWithoutScale = cellOriginY + cellHeightWithoutScale/2
//
//        return cellCenterYWithoutScale
//    }
//
//
//    class func __cellCenterYWithScale(cellCenterYWithoutScale: CGFloat, cellHeightWithoutScale: CGFloat, scale:CGFloat) -> CGFloat {
//        let cellCenterY = cellCenterYWithoutScale + ( cellHeightWithoutScale / 2 * (1 - scale))
//
//        return cellCenterY
//    }
    
    
    /// 吸附到固定的位置
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        print("offset:\(proposedContentOffset)")
        var targetContentOffset = proposedContentOffset
        let (total,more) = divmod(targetContentOffset.y, b: cellCenterOffset_y)
        if more > 0.0 {
            if more >= cellCenterOffset_y / 2.0 {
                targetContentOffset.y = ceil(total) * cellCenterOffset_y
            }
            else {
                targetContentOffset.y = floor(total) * cellCenterOffset_y
            }
        }
        return targetContentOffset
    }
    
    func divmod(_ a:CGFloat,b:CGFloat) -> (quotient:CGFloat, remainder:CGFloat){
        return (a / b, a.truncatingRemainder(dividingBy: b))
    }
}
